<?php
// +----------------------------------------------------------------------
// | 有客商城 [ 山西幺九网络科技有限公司 版权所有 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.yuuck.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 燕南天 <81020302@qq.com> 2018/7/15/015 9:26
// +----------------------------------------------------------------------

namespace app\admin\service;

use app\admin\model\Category;
use think\Exception;

class CategoryService
{
    private $model;

    public function __construct()
    {
        $this->model = new Category();
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $map
     * @param string $order
     * @param array $mapOr
     * @return array
     * @throws \think\exception\DbException
     */
    public function getList($page, $limit, $map=[], $order='id desc', $mapOr=[]){
        $tmp = $this->model->where($map);
        !empty($whereOr) && $tmp = $tmp->whereOr($mapOr);
        $field = 'id,catname,parentid,title,description,sort,hits,child,is_show';
        $data = $tmp
            ->field($field)
            ->order($order)
            ->paginate(array('list_rows'=>$limit,'page'=>$page))
            ->toArray();

        return $data;
    }

    /**
     * 添加文章
     * @param $data
     * @return array|bool
     * @throws \think\exception\PDOException
     */
    public function add($data)
    {
        try{
            $this->model->startTrans();
            $res = $this->model->isUpdate(false)->save($data);
            $this->model->commit();
            return $res;
        }catch (Exception $e){
            $this->model->rollback();
//            throw new \Exception("栏目添加失败");
            return ['code'=>'1008','msg'=>'栏目添加失败','data'];
        }
    }

    /**
     * 修改
     * @param $data
     * @param $map
     * @return array|bool
     * @throws \think\exception\PDOException
     */
    public function upd($data, $map)
    {
        try{
            $this->model->startTrans();
            $res = $this->model->isUpdate(true)->save($data, $map);
            $this->model->commit();
            return $res;
        }catch (Exception $e){
            $this->model->rollback();
//            throw new \Exception("栏目修改失败");
            return ['code'=>'1008','msg'=>'栏目修改失败','data'];
        }
    }


    public function getInfo($id){
        $data = $this->model->get($id);
        return $data;
    }

}